﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;

namespace Core
{
    public static class Main
    {
        public readonly static List<Field> Plot = new List<Field>();
        public static int Money = 250;
        private static readonly Timer Timer = new Timer(1000);

        private static void PreparePlot()
        {
            for (var i = 0; i < 4; i++)
            {
                Plot.Add(new Field {Empty = true, ID = i});
            }
        }

        public static int PutPlant(int fieldID, PlantType type)
        {
            var price = new Plant {Type = type}.Price;

            if (Money < price) return 1;

            Plot.First(p => p.ID == fieldID).Put(type);
            Money -= price;
            return 0;
        }

        public static void Start()
        {
            PreparePlot();
            PutPlant(1, PlantType.Potato);
            PutPlant(2, PlantType.Flower);
            Timer.Enabled = true;
            Timer.Elapsed += CheckPlot;
        }

        private static void CheckPlot(object sender, ElapsedEventArgs e)
        {
            for (var i = 0; i < Plot.Count; i++)
            {
                if (!Plot[i].Empty)
                {
                    var timeToEnd = Plot[i].Plant.PlantingDate
                        .AddMinutes(Plot[i].Plant.TimeToGrowup);
                    if (DateTime.Now >= timeToEnd)
                    {
                        Money += (int)(Plot[i].Plant.Price * 1.5);
                        Plot[i].Empty = true;
                    }
                }
            }
        }
    }
}
